package com.xinqi.modules.sys.dict.controller.app;

import com.xinqi.common.base.response.PageInfo;
import com.xinqi.common.base.response.R;
import com.xinqi.modules.sys.dict.dto.req.ConfigCreateDTO;
import com.xinqi.modules.sys.dict.dto.req.ConfigQueryDTO;
import com.xinqi.modules.sys.dict.dto.req.ConfigUpdateDTO;
import com.xinqi.modules.sys.dict.dto.rsp.ConfigResultDTO;
import com.xinqi.modules.sys.dict.service.ConfigService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 系统配置表APP控制层
 *
 * @author: linkai
 * @date: 2023/05/21
 */
@Tag(name = "App-系统配置表", description = "系统配置表App接口")
@RestController
@RequestMapping("/app/config")
@RequiredArgsConstructor
public class ConfigAppController {
    private final ConfigService configService;

    /**
     * 系统配置表-分页
     */
    @Operation(summary = "系统配置表-分页")
    @GetMapping("/page")
    public R<PageInfo<ConfigResultDTO>> page(@ParameterObject ConfigQueryDTO dto) {
        PageInfo<ConfigResultDTO> page = configService.page(dto, dto);
        return R.data(page);
    }

    /**
     * 系统配置表-详情
     */
    @Operation(summary = "系统配置表-详情")
    @GetMapping("/{id}")
    public R<ConfigResultDTO> findById(@Parameter(description = "id", example = "100001") @PathVariable("id") Long id) {
        ConfigResultDTO dto = configService.findById(id);
        return R.data(dto);
    }

    /**
     * 新增系统配置表
     */
    @Operation(summary = "新增系统配置表")
    @PostMapping
    public R<Long> insert(@RequestBody ConfigCreateDTO dto) {
        return R.data(configService.insert(dto));
    }

    /**
     * 修改系统配置表
     */
    @Operation(summary = "修改系统配置表")
    @PutMapping("/{id}")
    public R<Boolean> update(@Parameter(description = "id", example = "100001") @PathVariable("id") Long id,
                             @RequestBody ConfigUpdateDTO dto) {
        dto.setId(id);
        return R.data(configService.updateById(dto));
    }

    /**
     * 删除系统配置表
     */
    @Operation(summary = "删除系统配置表")
    @DeleteMapping("/{id}")
    public R<Boolean> delete(@Parameter(description = "id", example = "100001") @PathVariable("id") Long id) {
        return R.data(configService.deleteById(id));
    }
}
